<template>
  <div class="signup-container">
    <el-form ref="signupForm" :model="signupForm" :rules="signupRules" class="signup-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          宿舍管理系统
          <span class="title-right">
            已有账号？
            <router-link to="/login">
              <span>点我登录</span>
            </router-link>
          </span>
        </h3>
      </div>

      <el-form-item prop="sno">
        <span class="svg-container">
          <svg-icon icon-class="sysUser" />
        </span>
        <el-input
          ref="sno"
          v-model="signupForm.sno"
          placeholder="学号"
          name="sno"
          type="text"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="signupForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="confirmPassword">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="signupForm.confirmPassword"
          :type="passwordType"
          placeholder="确认密码"
          name="password"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="phone">
        <span class="svg-container">
          <svg-icon icon-class="phone" />
        </span>
        <el-input
          ref="phone"
          v-model="signupForm.phone"
          placeholder="手机"
          name="phone"
          type="text"
        />
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="code" />
        </span>
        <el-input
          ref="code"
          v-model="signupForm.code"
          placeholder="验证码"
          name="code"
          type="text"
        />
        <span class="checkCode">
          <el-button class="pan-btn green-btn" :disabled="!sending" @click="getCheckCode()">
            {{ codeTest }}
          </el-button>
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleSignup">注册</el-button>

      <!-- <div class="tips">
        <span style="float: right">注册</span>
        <span> password: any</span>
      </div> -->

    </el-form>
  </div>
</template>

<script>
import { getCode, signup } from '@/api/signup'
export default {
  name: 'Signup',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.signupForm.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      signupForm: {
        sno: '',
        phone: '',
        password: '',
        confirmPassword: '',
        code: ''
      },
      signupRules: {
        sno: [{ required: true, message: '学号不能为空', trigger: 'blur' },
          {
            pattern: /^[0-9]{11}$/,
            message: '请输入11位数字',
            trigger: 'blur'
          }],
        phone: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          {
            pattern: /^(\w){6,20}$/,
            message: '只能输入6-20个字母、数字、下划线',
            trigger: 'blur'
          }],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { required: true, validator: equalToPassword, trigger: 'blur' }
        ],
        code: [{ required: true, message: '验证码不能为空', trigger: 'blur' },
          {
            pattern: /^[0-9]{6}$/,
            message: '请输入6位数字',
            trigger: 'blur'
          }]
      },
      loading: false,
      passwordType: 'password',
      second: 60,
      codeTest: '获取验证码',
      sending: true
    }
  },
  // watch: {
  //   $route: {
  //     handler: function(route) {
  //       this.redirect = route.query && route.query.redirect
  //     },
  //     immediate: true
  //   }
  // },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    timeDown() {
      const result = setInterval(() => {
        --this.second
        this.codeTest = this.second + 's'
        if (this.second < 1) {
          clearInterval(result)
          this.sending = true
          this.second = 60
          this.codeTest = '获取验证码'
        }
      }, 1000)
    },
    getCheckCode() {
      this.$refs['signupForm'].validateField('phone', (phoneError) => {
        if (!phoneError) {
          getCode(this.signupForm.phone).then((response) => {
            this.sending = false
            this.timeDown()
            this.$message({
              message: '验证码：' + response.data,
              type: 'success',
              duration: 0,
              showClose: true
            })
          })
        }
      })
    },
    handleSignup() {
      this.$refs.signupForm.validate(valid => {
        if (valid) {
          // this.loading = true
          // this.$store.dispatch('user/signup', this.signupForm).then(() => {
          //   this.$router.push({ path: this.redirect || '/' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          // })
          signup(this.signupForm).then(() => {
            // this.$router.replace({ path: '/login' })
            this.$message({
              message: '注册成功',
              type: 'success'
            })
            this.loading = true
            setTimeout(() => {
              this.loading = false
              this.$router.replace({ path: '/login' })
            }, 1.5 * 1000)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .signup-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.signup-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.checkCode {
  position: absolute;
  // right: 10px;
  // top: 7px;
  right: 0px;
  // font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}
.pan-btn {
  height: 52px;
  border-radius: 0;
}

.signup-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .signup-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      // text-align: center;
      font-weight: bold;

      .title-right {
        float: right;
        font-size: 14px;
        padding-top: 12px;
        color: gray;
      }

    }

  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
